const path = require('path');
const fs = require('fs');
const prettier = require('prettier');
const docgen=require('./react-docgen-typescript/index');
const paths=require("../config/paths");
const {readFile}=require("./util")
async function toMarkDown(content){
    let str="";
    content.reverse().forEach((item)=>{
        const {displayName,description,props,methods}=item;
        methodsRender(methods);
        const title=displayName?`#组件 ${displayName}`:"";
        const propsDoc=prettier.format(propsRender(props),{
            parser: 'markdown'
        })
        const methodsDoc=prettier.format(methodsRender(methods),{
            parser: 'markdown'
        })
        const content=title+"\n"+`${blockMd(description)}`+"\n"+propsDoc+"\n"+methodsDoc;
         str+=content;
    })
   return str;
}
function addText(packageName){
    return`### 安装
    npm install ${packageName}
    yarn add ${packageName}`
}
function blockMd(text){
    const blockText="```"
    return blockText+"\n"+text+"\n"+blockText+"\n";
}
async function examples(name){
    const exampleFile=fs.readFileSync(path.join(paths.examples,name,"index.tsx")).toString();
    const strArray=exampleFile.split(/\r|\n/);
    let str="";
    strArray.forEach((item)=>{
        const itemStr=item.trim();
        if (itemStr!==""){
            str+="\n"+itemStr
        }
    })
    return `### 示例\n${blockMd(str)}`
}

function propsRender(props){
   return `### 参数 Props
  | 属性 |  类型 | 默认值 | 必填 | 描述 |
  | --- | --- | --- | --- | ---|
   ${Object.keys(props)
         .map((key) => renderProp(props[key]))
         .join('')}
  `
}
function methodsRender(methods){
 if (methods.length){
   return  `### 方法
  | 方法名 | 参数 | 返回值 | 描述 |
  | ----- | --- | ----- |---- | 
     ${methods.map((item)=>{
              return renderMethods(item)
         }).join("")}  
         `
     }
   return ""
}
function renderMethods(data){
     const {description,name,returns:{description:rDoc,type},params}=data;
     const paramsStr=params.map((item)=>{
          const {name:paramsName,description:paramsDoc,type}=item;
          return `参数名:${paramsName.replace("?","")}  描述:${paramsDoc} ${type.name?'类型:'+type.name:""} 必传:${paramsName.endsWith("?") ?'✗':'✓'}`
     }).join("\n");

     return `| ${name} | ${paramsStr} |描述：${rDoc} 类型:${type} |  ${description || '-'} |
     `
}
function renderProp(data){
    const {name,defaultValue,description,required,type:{name:Typename}}=data;
        return `| ${name} | ${Typename.split("|").join(",")} | ${defaultValue===null?"无": defaultValue.value} | ${required ? '✓' : '✗'} |  ${description || '-'} |
        `
}
async function cli(name){
    const content = docgen.parse(path.join(paths.components,name,"index.tsx"));
    let str=await toMarkDown(content,name);
    const packageJson=await readFile(path.join(paths.libOutput,name,"package.json"));
    str+="\n"+addText(packageJson.name)
    const exampleFile=await examples(name);
    str+='\n'+exampleFile;
    await fs.writeFileSync(path.join(paths.libOutput,name,"README.md"),str);
    console.log("文档生成完毕")
}
module.exports=cli